package cn.wikiflyer.beikang.act.user;

import java.lang.reflect.Type;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import net.tsz.afinal.annotation.view.ViewInject;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import cn.wikiflyer.beikang.BaseActivity;
import cn.wikiflyer.beikang.R;
import cn.wikiflyer.beikang.act.user.bean.BKAddressResultBean;
import cn.wikiflyer.beikang.bean.base.BKBaseBean;
import cn.wk.libs4a.WKApplication;
import cn.wk.libs4a.net.WKAsyncTaskPro;
import cn.wk.libs4a.view.WKHeader;

/**
 * 地址详情
 * @author qunlee_mr
 *
 */
public class UserAddressInfoAct extends BaseActivity implements OnClickListener {
	
	@ViewInject(id = R.id.header)
	private WKHeader header;
	@ViewInject(id = R.id.address_info_name)
	private TextView name;
	@ViewInject(id = R.id.address_info_area)
	private TextView area;
	@ViewInject(id = R.id.address_info_detail)
	private TextView detail;
	@ViewInject(id = R.id.address_info_phone)
	private TextView phone;
	
	private BKAddressResultBean bean = new BKAddressResultBean();

	@Override
	public void loadXml() {
		setContentView(R.layout.bk_act_user_address_info);
	}
 
	@Override
	public void init() {
		header.setLeftImg("返回", R.drawable.user_addback_img, true, 12);
//		header.setLeftImg("保存", R.drawable.user_add_save_img, true, 12);
		header.setClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
		}, null, null);
		
		Intent intent = getIntent();
		bean = (BKAddressResultBean) intent.getSerializableExtra("addressbean");
//		bean.setName(intent.getStringExtra("name"));
//		bean.setId(intent.getStringExtra("id"));
//		bean.setPhone(intent.getStringExtra("phone"));
//		bean.setProvince(intent.getStringExtra("province"));
//		bean.setCity(intent.getStringExtra("city"));
//		bean.setArea(intent.getStringExtra("area"));
//		bean.setAddress(intent.getStringExtra("detail"));
	}

	@Override
	public void setData() {
//		name.setText(bean.name);
//		area.setText(bean.province + bean.city + bean.area);
		detail.setText(bean.address);
		phone.setText(bean.phone);
	}

	@Override
	public void onClick(View arg0) {
		int id = arg0.getId();
		switch (id) {
		case R.id.address_info_setdef:
//			WKApplication.showMsg("设置默认");
			new DefaultAddressTask(ctx);
			break;
		case R.id.address_info_update:
			Intent intent = new Intent(ctx, UserAddressAAndUAct.class);
			intent.putExtra("addressbean", bean);
			startActivityForResult(intent, 0);
			break;
		case R.id.address_info_del:
//			WKApplication.showMsg("删除地址");
			new DeleteAddressTask(ctx);
			break;

		default:
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (data == null)
			return;
		if (resultCode == 0) {
			String name = data.getStringExtra("name");
			String phone = data.getStringExtra("phone");
			String province = data.getStringExtra("phone");
			String city = data.getStringExtra("city");
			String area = data.getStringExtra("area");
			String detail = data.getStringExtra("detail");
			this.name.setText(data.getStringExtra("name"));
			this.phone.setText(data.getStringExtra("phone"));
			this.area.setText(province + city + area);
			this.detail.setText(detail);
			
//			bean.setName(name);
//			bean.setAddress(detail);
//			bean.setPhone(phone);
//			bean.setProvince(province);
//			bean.setCity(city);
//			bean.setArea(area);
		}
	}
	/**删除收货地址
	 */
	class DeleteAddressTask extends WKAsyncTaskPro{
		public DeleteAddressTask(Context context) {
			super(context);
		}
		@Override
		protected String doInBackground(boolean... params) {
			return app().net.deleteAddress("1");
		}
		@Override
		protected void doOnPostExecute(String result, boolean isLocal) {
			Type cvbType = new TypeToken<BKBaseBean>() {
			}.getType();
			Gson gson = new Gson();
			BKBaseBean base = gson.fromJson(result, cvbType);
			if (base.status == 1) {
				WKApplication.showToast("删除地址成功.");
				activity.finish();
			}else{
				WKApplication.showToast("删除地址失败."+base.info);
			}
		}
	}
	/**设为默认的收货地址
	 */
	class DefaultAddressTask extends WKAsyncTaskPro{
		public DefaultAddressTask(Context context) {
			super(context);
		}
		@Override
		protected String doInBackground(boolean... params) {
			return app().net.defaultAddress("1");
		}
		@Override
		protected void doOnPostExecute(String result, boolean isLocal) {
			Type cvbType = new TypeToken<BKBaseBean>() {
			}.getType();
			Gson gson = new Gson();
			BKBaseBean base = gson.fromJson(result, cvbType);
			if (base.status == 1) {
				WKApplication.showToast("设置默认地址成功.");
			}else{
				WKApplication.showToast("设置默认地址失败."+base.info);
			}
		}
	}
	
	

}
